$(function() {
	var bussinessId = $("#identify").val();
	// alert(bussinessId);

	initPage(bussinessId);

});
var pageInfo={};
$("#selectAll").click(function() { 
	for (var i = 0; i < pageInfo.length; i++){
		 $("#"+pageInfo[i].id+"_check").prop("checked",true);
		}
	}); 
$("#cancelAll").click(function() { 
	for (var i = 0; i < pageInfo.length; i++){
		 $("#"+pageInfo[i].id+"_check").prop("checked",false);
		}
	}); 
$("body").on("click", ".extendimg", function (){
	jQuery("#avatarbig").prop("src","");
    var id=$(this).attr('id');
    jQuery("#avatarbig").prop("src",$("#"+id+"_img").attr("src"));
    
});
//展示详细信息
$("body").on("click",".hrefClick",function() {
	$("#xmxqList").empty();
	$("#xmxqList").append('<li data-role="list-divider" class="list-divider-ui">问题清单</li>');
	// 初始化面板信息
	var inspectId = $(this).attr("defineId");// 自定义的属性,方便id的传递

	$.ajax({
			url : window.rootPath
					+ '/ui/bussinessInspection/findById/'
					+ inspectId,
		
			dataType : 'json',
			type : 'get',
			success : function(result) {
				if (result.code == 1) {
					var info = result.data.info;
					// 构建面板数据
					$("#bussinessName")
							.html(
									info.businesssellerBaseInfo.shopName);
					$("#bussiessAddr")
							.text(
									info.businesssellerBaseInfo.shopAdress);
					$("#bussinessMng")
							.text(
									info.businesssellerBaseInfo.managementPerson.name);
					$("#remarkI").text(info.remark);
					
					$("#inspectCondition").text(
							info.inspectCondition.title);
					
					$("#inspectDate").text(moment(info.inspectDate).format('YYYY-MM-DD HH:mm'));
					$("#inspectPerson").text(info.user.name);
					
					var imgUrl=info.pictureAddr;
					if(imgUrl==undefined||imgUrl==""){
						imgUrl="../img/nopicture.png";
					}else{
						imgUrl=window.rootPath+'/'+imgUrl;
					}
					$("#avatartI").attr("src",imgUrl);
					
					
					var list=info.details;
					
					var enr='';
					if(list==null||list.length==0){
						var  contenthtml =   '<tr><td colspan="2">'  
			                  +'<h2 style="color:blue">暂无巡检问题清单</h2>'  
			                  + '</tr></td>';  
			                 // $("#xmxqList").append(contenthtml);
					}
					for (var i = 0; i < list.length; i++) {
						var purl=list[i].picAddr;
						if(purl==undefined||purl==""){
							purl="../img/nopicture.png";
						}else{
							purl=window.rootPath+'/'+purl;
						}
						
						var  contenthtml ='<tr><td colspan="2" style="color: #999999; border-bottom: 2px solid #55B937 !important;"></td></tr>'
						  +'<tr><td width="1%"><p style="color: #999999;">现场图片</p></td><td><a href="#pop" data-rel="popup" data-position-to="window" id="'+list[i].id+'" class="extendimg" data-transition="fade"><img id="'+list[i].id+'_img" width="30%" src="'+purl+'"/></a></td></tr>'  
						  +'<tr><td><p style="color: #999999;">备注</p></td><td><p>'+list[i].remark+'</p></td></tr>' 
		                  +'<tr><td><p style="color: #999999;">时间</p></td><td><p>'+moment(list[i].createTime).format('YYYY-MM-DD HH:mm')+'</p></td></tr>';
		               //  $("#xmxqList").append(contenthtml);
						 var infoId = list[i].id;
			                var memo1 = "备注:"+list[i].remark;
			                var memo2 = "时间:"+moment(list[i].createTime).format('YYYY-MM-DD HH:mm');
			               $("#xmxqList").setListViewContent(infoId,purl,memo1,memo2);
					}
					for(var i=0;i<list.length;i++){
						
						var scrUrl=rootPath+"/"+list[i].picAddr;
						/*$("#xmxqList").append("<li><table><tr>" +
								"<td width='3%'>" +
								"<img with='3%' src="+scrUrl+"/>" 
								+"</td>" +
								"<td><font color='red'>"+list[i].inspectOpt.title+"</td>" +
								"<tr></table></li>");*/
					}
					$("#inspectEnvir").append(enr);
				    $("#inspectEnvirResult").text(info.environmentOpt.title);
				}
			}
		});
	// 指向面板属性的id
	$(this).prop("href", "#detailInspectPanel");

});
function initPage(bussinessId) {
	$.ajax({
				url : window.rootPath
						+ '/ui/bussinessCheck/pageByTimeOnPhone?device=mobile&&identify='
						+ bussinessId,
				dataType : 'json',
				type : 'get',
				success : function(result) {
					 pageInfo = result.data.pageInfo;
					var pageHistoryInfo=result.data.pageHistoryInfo;
					if (result.code == 1) {
						// 构建待考核数据
						if(pageInfo.length==0){
							/*var  contenthtml =   '<li>'  
                          +'<h2 style="color:blue">暂无待考核项</h2>'  
                          + '</li>';  
                          $("#waitingchecklistul").append(contenthtml);
                          $('#waitingchecklistul').listview('refresh');*/
							
							/*var  contenthtml =   '<tr><td colspan="2">'  
				                  +'<h2 style="color:blue">暂无待考核记录</h2>'  
				                  + '</tr></td>';  
				                  $("#mywaitingCheckTable").append(contenthtml);*/
							var  contenthtml =   '<li>'  
		                          +'<h2 style="color:blue">暂无待考核记录</h2>'  
		                          + '</li>';  
		                          $("#waitingchecklistul").append(contenthtml);
		                          $('#waitingchecklistul').listview('refresh');
						}
						if(pageHistoryInfo.length==0){
							var  contenthtml =   '<li>'  
                          +'<h2 style="color:blue">暂无考核历史</h2>'  
                          + '</li>';  
                          $("#historychecklistul").append(contenthtml);
                          $('#historychecklistul').listview('refresh');
						}
						
						for (var i = 0; i < pageInfo.length; i++) {
							var purl=pageInfo[i].pictureAddr;
							if(purl==undefined||purl==""){
								purl="../img/nopicture.png";
							}else{
								purl=window.rootPath+'/'+purl;
							}
							/*var title=pageInfo[i].inspectCondition.title;
							var titleColor="";
							
							if(title=="优秀"){
								titleColor="green";
							}else if(title="良好"){
								titleColor="blue";
							}else if(title="及格"){
								titleColor="yellow";
							}else if(title="不及格"){
								titleColor="red";
							}*/
							
							/*var  contenthtml ='<tr><td><img width="30%" src="'+purl+'" /></td><td><input type="checkbox" name="favcolor" id="'+pageInfo[i].id+'" value="red">选择</input></td></tr>'  
							  +'<tr><td><p>巡检人:</p></td><td><p>'+pageInfo[i].user.name+'</p></td></tr>' 
			                  +'<tr><td><p>时间：</p></td><td><p style="color:blue">'+moment(pageInfo[i].inspectDate).format('YYYY-MM-DD')+'</p></td></tr>'
			                  +'<tr><td><p>总体评价:</p></td><td><p style="color:'+titleColor+'">'+title+'</p></td></tr>'
							  +'<tr><td></td></tr>';
			                  $("#mywaitingCheckTable").append(contenthtml);*/
							var listView=$("#waitingchecklistul");
							var infoId = pageInfo[i].id;
							var pic = purl;
							var memo1 = moment(pageInfo[i].inspectDate).format('YYYY-MM-DD HH:mm');
							var memo2 = '总体评价:'+pageInfo[i].inspectCondition.title;
							//var memo3 = tempHtml;
							var memo4 = '巡检人:'+pageInfo[i].user.name;
							var memo5 = '电话:'+pageInfo[i].user.contactType;
							listView.setListViewContent(infoId,pic,memo1,memo2,memo4,memo5);
							/*var contenthtml='<br/><div class="div1">'
								+'<a href="#detailInspectPanel" data-transition="slide" class="hrefClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'"><div class="div-time"><div style="margin-left:20px;">'
								+moment(pageInfo[i].inspectDate).format('YYYY-MM-DD HH:mm')
								+'</div></div></a><br/>'
							    +'<div class="div-type"><input type="checkbox" name="favcolor" id="'+pageInfo[i].id+'_check" value="red">选择</input></div><br/>'
							    +'<div class="div-type">总体评价:'+pageInfo[i].inspectCondition.title+'</div><br/>';
								if(pageInfo[i].details!=null){
									for(var j=0;j<pageInfo[i].details.length;j++){
										var str =pageInfo[i].details[j].inspectOpt.title;
										if(str.length>10){
											str =str.substring(0,12)+"...";
										}
										if(j<pageInfo[i].details.length-1){
											contenthtml+='<div class="div-type">'+str+'</div><br/>';
										}else{
											contenthtml+='<div class="div-type">'+str+'</div>';
										}
										
									}
								}else{
									var str =pageInfo[i].remark;
									if(str.length>10){
										str =str.substring(0,12)+"...";
									}
									contenthtml+='<div class="div-type">'+str+'</div>';
								}
								contenthtml+='<div style="overflow: auto;padding-top:0;margin-left:20px;">'
								+'<div class="div-inline"><p style="margin:0;">巡检人:'+pageInfo[i].user.name+'</p></div>'
								+'<div class="div-inline"><p style="margin:0;">电话:'+pageInfo[i].user.contactType+'</p></div></div>'
								+'</div>';
								$("#waitingchecklistul").append(contenthtml);*/
						}
						//构建历史考核数据
						for (var i = 0; i < pageHistoryInfo.length; i++) {
							var purl=pageHistoryInfo[i].pictureAddr;
							if(purl==undefined||purl==""){
								purl="../img/nopicture.png";
							}else{
								purl=window.rootPath+'/'+purl;	
							}
							var str="";
							if(pageHistoryInfo[i].details!=null){
								for(var j=0;j<pageHistoryInfo[i].details.length;j++){
									var str =pageHistoryInfo[i].details[j].checkTypeInfo.title;
									if(str.length>10){
										str =str.substring(0,12)+"...";
									}
									if(j<pageHistoryInfo[i].details.length-1){
										//contenthtml+='<div class="div-type">'+str+'</div><br/>';
									}else{
										//contenthtml+='<div class="div-type">'+str+'</div>';
									}
									
								}
							}else{
								var str =pageHistoryInfo[i].checkCondition;
								if(str.length>10){
									str =str.substring(0,12)+"...";
								}
								//contenthtml+='<div class="div-type">'+str+'</div>';
							}
							var listView=$("#historychecklistul");
							var infoId = pageHistoryInfo[i].id;
							var pic = purl;
							var memo1 = '<font style="color:blue">'+moment(pageHistoryInfo[i].checkDate).format('YYYY-MM-DD')+'</font>'  
							//var memo2 = 
							var memo3 = str;
							var memo4 = '考核人:'+pageHistoryInfo[i].user.name;
							var memo5 = '整改情况:'+pageHistoryInfo[i].checkCondition;  
							listView.setListViewContent(infoId,pic,memo1,memo3,memo4,memo5);
							/*var  contenthtml =   '<li>'+' <a href="#sPanel" data-transition="slide" class="aclick" id="'+pageHistoryInfo[i].id+'">'  
                            +'<img width="80%" src="'+purl+'" />'  
                          +'<h2>考核人:'+pageHistoryInfo[i].user.name+'</h2>'
                          +'<h2>整改情况:'+pageHistoryInfo[i].checkCondition+'</h2>'  
                          +'<p style="color:blue">时间：'+moment(pageHistoryInfo[i].checkDate).format('YYYY-MM-DD')+'</p>'  
                          + '</a>' + '</li>';  
                          $("#historychecklistul").append(contenthtml);
                          $('#historychecklistul').listview('refresh');*/
                          
						/*var contenthtml='<br/><div class="div1">'
						+'<a href="#sPanel" data-transition="slide" class="aclick" id="'+pageHistoryInfo[i].id+'"><div class="div-time"><div style="margin-left:20px;">'
						+moment(pageHistoryInfo[i].checkDate).format('YYYY-MM-DD HH:mm')
						+'</div></div></a><br/>';
						if(pageHistoryInfo[i].details!=null){
							for(var j=0;j<pageHistoryInfo[i].details.length;j++){
								var str =pageHistoryInfo[i].details[j].checkTypeInfo.title;
								if(str.length>10){
									str =str.substring(0,12)+"...";
								}
								if(j<pageHistoryInfo[i].details.length-1){
									contenthtml+='<div class="div-type">'+str+'</div><br/>';
								}else{
									contenthtml+='<div class="div-type">'+str+'</div>';
								}
								
							}
						}else{
							var str =pageHistoryInfo[i].checkCondition;
							if(str.length>10){
								str =str.substring(0,12)+"...";
							}
							contenthtml+='<div class="div-type">'+str+'</div>';
						}
						contenthtml+='<div style="overflow: auto;padding-top:0;margin-left:20px;">'
						+'<div class="div-inline"><p style="margin:0;">核检人:'+pageHistoryInfo[i].user.name+'</p></div>'
						+'<div class="div-inline"><p style="margin:0;">电话:'+pageHistoryInfo[i].user.contactType+'</p></div></div>'
						+'</div>';
						$("#historychecklistul").append(contenthtml);*/
                        $('#waitingchecklistul').listview('refresh');
                        $('#historychecklistul').listview('refresh');
						}
					}
				}
			});
};
$("body").on("click", ".aclick", function (){
	$("#xxjsList").empty();
	$("#xxjsList").append('<li data-role="list-divider" class="list-divider-ui">详细介绍</li>');
	$("#xjlsList").empty();
	$("#xjlsList").append('<li data-role="list-divider" class="list-divider-ui">巡检历史</li>');
	/*$("#myDetailTable tr:not(:first)").remove();
	$("#myhistoryTable tr:not(:first)").remove();*/
	$("#myDetailTable tr").remove();
	$("#myhistoryTable tr").remove();
	var a=$(this).attr("id");
	$.ajax({
		url : window.rootPath
				+ '/ui/bussinessCheck/checkDetailInfo?device=mobile&&identify='
				+ a,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			
			var recordInfo=result.data.recordInfo;
			var detailInfo =result.data.detailInfo;
			var bussinessCheck=result.data.bussinessCheck;
			console.log(recordInfo);
			console.log(detailInfo);
			console.log(bussinessCheck);
			$("#checkDate").html(moment(bussinessCheck.checkDate).format('YYYY-MM-DD HH:mm'));
			$("#user").html(bussinessCheck.user.name);
			$("#checkCondition").html(bussinessCheck.checkCondition);
			$("#rectifyCondition").html(bussinessCheck.rectifyCondition);
			$("#remark").html(bussinessCheck.remark);
			
			var imgUrl=bussinessCheck.pictureAddr;
			if(imgUrl==undefined||imgUrl==""){
				imgUrl="../img/nopicture.png";
			}else{
				imgUrl=window.rootPath+'/'+imgUrl;
			}
			$("#avatartD").attr("src",imgUrl);
			
			if (result.code == 1) {
				// 巡检历史
				if(recordInfo.length==0){
					var  contenthtml =   '<tr><td colspan="2">'  
                  +'<h2 style="color:blue">暂无巡检历史记录</h2>'  
                  + '</tr></td>';  
                  $("#myhistoryTable").append(contenthtml);
				}
				//遗留详情
				if(detailInfo.length==0){
					var  contenthtml =   '<tr><td colspan="2">'  
		                  +'<h2 style="color:blue">暂无巡检核查详情</h2>'  
		                  + '</tr></td>';  
		                  $("#myDetailTable").append(contenthtml);
				}
				
				for (var i = 0; i < recordInfo.length; i++) {
					var purl=recordInfo[i].pictureAddr;
					if(purl==undefined||purl==""){
						purl="../img/nopicture.png";
					}else{
						purl=window.rootPath+'/'+purl;
					}
					var title=recordInfo[i].inspectCondition.title;
					var titleColor="";
					
					if(title=="优秀"){
						titleColor="green";
					}else if(title="良好"){
						titleColor="blue";
					}else if(title="及格"){
						titleColor="yellow";
					}else if(title="不及格"){
						titleColor="red";
					}
					var  contenthtml ='<tr><td colspan="2" style="color: #999999; border-bottom: 2px solid #55B937 !important;"></td></tr>'
				  +'<tr><td width="15%"><p style="color: #999999;">现场图片</p></td><td><a href="#pop" data-rel="popup" data-position-to="window" id="'+recordInfo[i].id+'" class="extendimg" data-transition="fade"><img width="30%" id="'+recordInfo[i].id+'_img" src="'+purl+'" /></a></td></tr>'  
				  +'<tr><td><p style="color: #999999;">巡检人</p></td><td><p>'+recordInfo[i].user.name+'</p></td></tr>' 
                  +'<tr><td><p style="color: #999999;">时间</p></td><td><p style="color:blue">'+moment(recordInfo[i].inspectDate).format('YYYY-MM-DD HH:mm')+'</p></td></tr>'
                  +'<tr><td><p style="color: #999999;">总体评价</p></td><td><p style="color:'+titleColor+'">'+title+'</p></td></tr>';
                  $("#myhistoryTable").append(contenthtml);
                  
                  var infoId = recordInfo[i].id;
                	var memo4="巡检人:"+recordInfo[i].user.name;
                   var memo1 = "总体评价:"+title;
                  var memo2 = "时间:"+moment(recordInfo[i].inspectDate).format('YYYY-MM-DD HH:mm');
                  $("#xjlsList").setListViewContent(infoId,purl,memo2,memo1,memo4);
				}
				//构建历史考核数据
				for (var i = 0; i < detailInfo.length; i++) {
					var purl=detailInfo[i].pictureAdress;
					if(purl==undefined||purl==""){
						purl="../img/nopicture.png";
					}else{
						purl=window.rootPath+'/'+purl;
					}
					
					var  contenthtml ='<tr><td colspan="2" style="color: #999999; border-bottom: 2px solid #55B937 !important;"></td></tr>'
					  +'<tr><td width="15%"><p style="color: #999999;">现场图片</p></td><td><a href="#pop" data-rel="popup" data-position-to="window" id="'+detailInfo[i].id+'" class="extendimg" data-transition="fade"><img id="'+detailInfo[i].id+'_img" width="30%" src="'+purl+'"/></a></td></tr>'  
					  +'<tr><td><p style="color: #999999;">问题</p></td><td><p>'+detailInfo[i].checkTypeInfo.title+'</p></td></tr>' 
					  +'<tr><td><p style="color: #999999;">是否整改</p></td><td><p>'+detailInfo[i].repairInfo+'</p></td></tr>'
					  +'<tr><td><p style="color: #999999;">备注</p></td><td><p>'+detailInfo[i].memo+'</p></td></tr>' 
	                  +'<tr><td><p style="color: #999999;">时间</p></td><td><p>'+moment(detailInfo[i].createTime).format('YYYY-MM-DD HH:mm')+'</p></td></tr>';
	                  $("#myDetailTable").append(contenthtml);
	                  	var infoId = detailInfo[i].id;
	                  	var memo3="问题:"+detailInfo[i].checkTypeInfo.title;
	                  	var memo4="是否整改:"+detailInfo[i].repairInfo;
	                     var memo1 = "备注:"+detailInfo[i].memo;
	                    var memo2 = "时间:"+moment(detailInfo[i].createTime).format('YYYY-MM-DD HH:mm');
	                    $("#xxjsList").setListViewContent(infoId,purl,memo2,memo1,memo4,memo3);
				}
			}
		}
	});
	});
$(document).ready(function() {
	$("#goHome").attr("href",$("#goHome").attr("href")+"&&identify="+$('#identify').val());
});
// 删除该条巡检,使用的是restful风格的DELETE提交方式
function deleteInspection(data) {

	if (confirm('确定删除该条巡检记录?')) {
		$.ajax({
			url : window.rootPath + '/ui/bussinessCheck/delete/' + data.id,
			dataType : 'json',
			type : 'get',
			data : '_method=DELETE',
			success : function(result) {
				// 返回成功gg
				if (result.code == 1) {
					// 放回商家的巡检页面
					window.location = window.rootPath
							+ '/ui/bussinessCheck?device=mobile&identify='
							+ data.businesssellerBaseInfo.id;
				}
			}
		});
	}
};

// 修改页面
function editInspection(data) {
	if (confirm('确定修改该条巡检记录?')) {
		// 修改页面
		window.location = window.rootPath + "/ui/bussinessCheck/saveOrUpdate/"
				+ data.id + "?device=mobile&identify="
				+ data.businesssellerBaseInfo.id;
	}
};

$("#saveCheckBtn").click(
		function() {
			var inspectionIdList="";
			for (var i = 0; i < pageInfo.length; i++){
			 if($("#"+pageInfo[i].id+"_check").is(":checked")){
				 inspectionIdList+=pageInfo[i].id+";";
			 }
			}
			if(inspectionIdList!=""){
				window.location = window.rootPath
				+ "/ui/bussinessCheck/saveOrUpdate?device=mobile&identify="
				+ $("#identify").val()+"&inspectionIdList="+inspectionIdList;	
			}else{
				$("#alterpop").popup("open");
			}
		});
